<template>
  <page-card title="职级管理"
             sub-title="编辑"
             :need-back="true">
    <a-form :form="form"
            :label-col="{ span: 6 }"
            :wrapper-col="{ span: 12 }">
      <a-form-item label="ID"
                   v-show="false">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item label="职级名称"
                   required>
        <a-input placeholder="请输入"
                 v-decorator="['occName',{rules: [{
                   required: true, message: '请输入职级名称'
                 }]}]" />
      </a-form-item>
      <a-form-item label="职级编码"
                   required>
        <a-input placeholder="请输入"
                 v-decorator="['occCode',{rules: [{
                   required: true, message: '请输入职级编码'
                 }]}]" />
      </a-form-item>
      <a-form-item label="职业等级"
                   required>
        <a-input-number placeholder="请输入"
                        :min="1"
                        :max="7"
                        v-decorator="['occLevel',{rules: [{
                          required: true, message: '请输入职业等级（1-7）'
                        }]}]" />
      </a-form-item>
      <a-form-item label="排序号"
                   required>
        <a-input-number placeholder="请输入"
                        :min="1"
                        :max="7"
                        v-decorator="['orderNo',{rules: [{
                          required: true, message: '请输入'
                        }]}]" />
      </a-form-item>
      <a-form-item label=" "
                   :colon="false">
        <div style="text-align:center">
          <a-button type="primary"
                    action
                    @click="handleSave">保存</a-button>
        </div>
      </a-form-item>
    </a-form>
  </page-card>
</template>

<script>
import { saveOrUpdateRankInfo, getOrderNo, getRankInfoById } from '../../api/job/rank'
export default {
  name: 'RankManagementEdit',
  data () {
    return {
      id: this.$route.query.id || '',
      form: this.$form.createForm(this)
    }
  },
  mounted () { },
  created () {
    this.getOrderNo()
    if (this.id != '') {
      this.getInfoByID(this.id)
    }
  },
  methods: {
    // 保存
    handleSave (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (err) {
          return
        }
        if (this.id != '') {
          values.id = this.id
        }
        saveOrUpdateRankInfo(values).then((res) => {
          this.$router.go(-1)
        })
      })
    },
    // 排序号
    getOrderNo () {
      getOrderNo().then((res) => {
        getOrderNo().then((response) => {
          this.form.setFieldsValue({
            orderNo: res
          })
        })
      })
    },
    // 根据id返回详细信息
    getInfoByID (id) {
      getRankInfoById(id).then(res => {
        this.form.setFieldsValue(res)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-input-number {
  width: 100%;
}
</style>